#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
IR 基本表达式节点定义
"""

from dataclasses import dataclass, field
from typing import Any
import ast
from .base import IRExpr
from ...symbol_table import Symbol, SymbolKind


@dataclass
class IRConstant(IRExpr):
    """常量节点"""

    value: Any = field(default=None)
    node_type: str = "constant"

    def build_from_ast(self, node: ast.Constant) -> None:
        """从 AST 常量节点构建 IR 常量节点内容

        Args:
            node: AST 常量节点
        """
        super().build_from_ast(node)

        self.value = node.value


@dataclass
class IRName(IRExpr):
    """名称节点（变量引用）"""

    id: str = field(default="")
    node_type: str = "name"

    def new_symbol(self) -> Symbol:
        return Symbol(
            self.id,
            SymbolKind.VARIABLE,
            line=self.source_line,
            ir_node=self,
        )

    def build_from_ast(self, node: ast.Name) -> None:
        """从 AST 名称节点构建 IR 名称节点内容

        Args:
            node: AST 名称节点
        """
        super().build_from_ast(node)
        self.id = node.id
